/*
 * Project: yygh-parent
 *
 * File Created at 2023/7/16
 *
 * 自定义时间格式代替DATE标签：2023-07-16 18:23:18:23
 */
package com.atguigu.yygh.order.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.repository.DepartmentRepository;
import com.atguigu.yygh.repository.HospitalRepository;
import com.atguigu.yygh.order.service.DepartmentService;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 用户名
 * @Type DepartmentServiceImpl.java
 * @Desc
 * @date 2023/7/16 18:23
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    HospitalRepository hospitalRepository;

    @Autowired
    DepartmentRepository departmentRepository;

    @Override
    public void saveDepartmentByHoscode(Map<String, Object> map) {
        String hoscode=(String)map.get("hoscode");
        Hospital hospitalByHoscode = hospitalRepository.getHospitalByHoscode(hoscode);
        if (hospitalByHoscode != null) {
            String s = JSONObject.toJSONString(map);
            Department department = JSONObject.parseObject(s, Department.class);
            BeanUtils.copyProperties(department,hospitalByHoscode);
            hospitalByHoscode.setCreateTime(new Date());
            hospitalByHoscode.setIsDeleted(0);
            hospitalByHoscode.setUpdateTime(new Date());
            departmentRepository.save(department);
        }else{
            String s = JSONObject.toJSONString(map);
            Department department = JSONObject.parseObject(s, Department.class);
            department.setCreateTime(new Date());
            department.setIsDeleted(0);
            department.setUpdateTime(new Date());
                departmentRepository.save(department);

        }


    }

    @Override
    public Page<Department> selectPage(Integer page, Integer limit, DepartmentQueryVo departmentQueryVo) {
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
//0为第一页
        Pageable pageable = PageRequest.of(page-1, limit, sort);
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写
        //创建实例
        Example<Department> example = Example.of(department, matcher);

        Page<Department> pages = departmentRepository.findAll(example,pageable);

        return pages;
    }

    @Override
    public void removeByHoscodeAndDepcode(DepartmentQueryVo departmentQueryVo) {
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        Department dep = departmentRepository.findDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
        if(dep!=null){
            departmentRepository.deleteById(dep.getId());
        }

    }

    @Override
    public List<DepartmentVo> selectAllByHoscode(String hoscode) {
        //创建list集合，用于最终数据封装
        List<DepartmentVo> result = new ArrayList<>();

        //根据医院编号，查询医院所有科室信息
        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscode);
        Example example = Example.of(departmentQuery);
        //所有科室列表 departmentList
        List<Department> departmentList = departmentRepository.findAll(example);

        //根据大科室编号  bigcode 分组，获取每个大科室里面下级子科室
        Map<String, List<Department>> deparmentMap =
                departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //遍历map集合 deparmentMap
        for(Map.Entry<String,List<Department>> entry : deparmentMap.entrySet()) {
            //大科室编号
            String bigcode = entry.getKey();
            //大科室编号对应的全局数据
            List<Department> deparment1List = entry.getValue();
            System.out.println(deparment1List);

            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(bigcode);
            departmentVo.setDepname(deparment1List.get(0).getBigname());

            //添加孩子
            List<DepartmentVo> childs = new ArrayList<>();
            for (Department department : deparment1List) {
                DepartmentVo departmentVo1 = new DepartmentVo();
                BeanUtils.copyProperties(department,departmentVo1);
                childs.add(departmentVo1);
            }

            departmentVo.setChildren(childs);

            result.add(departmentVo);
        }
        //返回
        return result;
    }




}


/**
 * Revision history
 * -------------------------------------------------------------------------
 * <p>
 * Date Author Note
 * -------------------------------------------------------------------------
 * 2023/7/16 用户名 create
 */ 
